var yearArr = getYearArr();
var _pInfo = {
	"userInfo": null,
	"yearSelectObj": null,
	"headImgFormData": null,
	"selectConfig": {
		trigger: "#year-box",
		title: "年龄",
		wheels: [{
			data: yearArr
		}],
		callback: function(indexArr, data) {
			$("#year").text(data[0]);
		},
		onShow: function(o) {
			var _year = $("#year").text();
			if(isNull(_year)) {
				o.locatePosition(0, 29);
				return;
			}
			o.locatePosition(0, $("#year").text() - 1);
		},
		triggerDisplayData: false
	},
	"param": getQueryParams(),
	"renderData": function() {
		AjaxRequest({
			url: _url + "/money/user/usermsg",
			type: "post",
			load: true,
			data: {
				"userid": _pInfo.userInfo["id"]
			},
			success: function(rs) {
				if(isReqSucceed(rs)) {
					_pInfo.userInfo = rs.data;
					$("#personal-body").html(template("personal-body-tpl", {
						rs: rs.data,
						url: imgUrl
					}));
					yearSelectObj = new MobileSelect(_pInfo.selectConfig);
				}
			}
		});
	},
	"getFormData": function(o) {
		var _formData = new FormData;
		var _file = $(o)[0].files[0];
		if(_file.type != "image/jpeg" && _file.type != "image/png" && _file.type != "image/jpg") {
			$.toast("请选择图片文件", "text");
			return;
		}
		var reader = new FileReader();              
		reader.readAsDataURL(_file);         
		reader.onloadend = function(e) {
			$("#head-img").attr("src", e.target.result);
		};   
		_formData.append("headURL", _file);
		this.headImgFormData = _formData;
	},
	"doEditUserData": function() {
		var _this = this;
		var _sendUrl = _url + "/money/user/userup?userid=" + _this.userInfo["id"];
		var nikename = $("#nikename").text().trim();
		if(!isNull(nikename)) {
			_sendUrl += "&nikename=" + nikename;
		}
		var sex = $("#sex").text().trim();
		if(!isNull(sex)) {
			var sexVal = 0;
			if(sex == "女") sexVal = 1;
			_sendUrl += "&sex=" + sexVal;
		}
		var year = $("#year").text().trim();
		if(!isNull(year)) {
			_sendUrl += "&age=" + year;
		}
		var opts = {
			url: _sendUrl,
			type: "post",
			load: true,
			upload: true,
			success: function(rs) {
				if(isReqSucceed(rs)) {
					$.alert("保存成功");
					_this.renderData();
				}
			}
		}
		if(_this.headImgFormData != null) {
			opts["data"] = _this.headImgFormData;
		}
		AjaxRequest(opts);
	},
	"getIptNickName": function() {
		var _nikename = $("#tpl-nikename").val().trim();
		if(isNull(_nikename)) {
			$.toast("请输入昵称", "text");
			return;
		}
		$.closePopup();
		$("#nikename").text(_nikename);
	},
	"showNicknamePopup": function() {
		$("#tpl-nikename").val(_pInfo.userInfo.nikename);
		$("#nickName-popup").popup();
	},
	"loadEvents": function() {
		_pInfo.userInfo = _user.getUserInfo();
	},
	"showSelectByType": function(type) {
		switch(type) {
			case "sex":
				$.actions({
					actions: [{
						text: "男",
						className: "action-color-blue",
						onClick: function() {
							$("#sex-box").html("<font color='#27A3FD' id='sex'>男</font>");
						}
					}, {
						text: "女",
						className: "action-color-red",
						onClick: function() {
							$("#sex-box").html("<font color='#FD2727' id='sex'>女</font>");
						}
					}]
				});
				break;
			default:
				break;
		}
	},
	"isUserAuth": function() {
		if(this.userInfo.iDNumber != null && this.userInfo.iDNumber != '') {
			$.toast("已完成实名认证", "text");
			return;
		}
		window.location.href = "become-member.html";
	},
	"load": function() {
		this.loadEvents();
		this.renderData();
	}
}
exposeGlobalMethods({
	"showSelectByType": function(type) {
		_pInfo.showSelectByType(type);
	},
	"doEditUserData": function() {
		_pInfo.doEditUserData();
	},
	"getIptNickName": function() {
		_pInfo.getIptNickName();
	},
	"getFormData": function(o) {
		_pInfo.getFormData(o);
	},
	"isUserAuth": function() {
		_pInfo.isUserAuth();
	},
	"showNicknamePopup": function() {
		_pInfo.showNicknamePopup();
	}
});
$(function() {
	_pInfo.load();
});

function getYearArr() {
	var arr = [];
	for(var i = 1; i <= 100; i++) {
		arr.push(i);
	}
	return arr;
}